#!/usr/bin/env ruby
# -*- coding: us-ascii -*-

require 'simulate18' if RUBY_VERSION < '1.8'

########################################

module Math
  # orcish maneuver in case math.rb gets loaded more than once
  A ||= (Math.sqrt(5) - 1) / 2
  
  def Math.h(k, m)
    kAmod1 = (k * A) % 1
    (m * kAmod1).floor
  end
end

########################################

if $0 == __FILE__
  dir = File.dirname(__FILE__)
  require File.join(dir, '..', '..', 'test', 'handy4testing')
  require File.join(dir, '..', '..', 'test', 'gibberish')
  
  include Handy4Testing
  include Gibberish
  
  puts Math::A
  s = 'this is a string of seven words'
  p [s, Math.h(s.hash, 1000)]
  a = s.split
  p [a, Math.h(a.hash, 1000)]
  
  # need 7 (or 14 for 8-byte machine) chars for >= Fixnum::MAX permutations
  # let's use 8 chars for 4-byte machine, 16 chars for 8-byte machine
  num_chars = RUBY_PLATFORM =~ /java/i ? 16 : 8
  
  # 50000
  stress(50) do
    word = wordn(num_chars)
    h = Math.h(word.hash, 1000)
    #p [word, h]
    raise RangeError, "BAD HASH: #{word}, #{h}" unless h>=0 and h<1000
  end
end
